import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path'
import { visualizer } from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const timestamp = new Date().getTime()
// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		vueJsx(),
		// 打包后查看资源占比,执行打包后会生成一个stas.html
		visualizer({
			open: true // true 打包完自动打开分析页面，false 不会自动弹出
			// filename: 'stats.html', // 分析图生成的文件名
			// gzipSize: true, // 是否统计并显示gzip
			// brotliSize: true, // 是否统计并显示brotli
		}),
		AutoImport({
			resolvers: [ElementPlusResolver({ importStyle: 'css' })],
			dts: 'types/auto-generate/auto-import.d.ts'
		}),
		Components({
			extensions: ['vue', 'ts', 'tsx'],
			resolvers: [ElementPlusResolver({ importStyle: 'css' })],
			dts: 'types/auto-generate/auto-components.d.ts'
		})
		// ElementPlus({ useSource: true }), // Element Plus 样式自动按需导入
	],
	resolve: {
		alias: {
			'@': resolve('./src') // 相对路径别名配置，使用 @ 代替 src
		}
	},

	base: './',
	build: {
		outDir: 'dist',
		minify: 'esbuild',
		reportCompressedSize: false,
		// 规定触发警告的chunk大小
		chunkSizeWarningLimit: 500,
		rollupOptions: {
			// external: ['element-plus'],
			output: {
				manualChunks(id) {
					if (id.includes('node_modules')) {
						return id.toString().split('node_modules/')[1].split('/')[0].toString()
					}
				},
				chunkFileNames: `assets/js/[name]-[hash]-${timestamp}.js`,
				entryFileNames: `assets/js/[name]-[hash]-${timestamp}.js`,
				assetFileNames: `assets/[ext]/[name]-[hash]-${timestamp}.[ext]`
			}
		}
	},
	server: {
		port: 8080,
		open: true,
		host: '0.0.0.0',
		proxy: {
			'/api': {
				target: 'http://ikun-pro-ui.fun:3000',
				// target: 'http://localhost:3000',
				changeOrigin: true,
				rewrite: path => path.replace(/^\/api/, '')
			}
		}
	}
})
